<template>
  <view class="query_container">
    <!-- 商品展示区域 -->
    <view class="list" v-if="!rule_show">
      <scroll-view
        :scroll-y="true"
        class="scroll-y"
        @scrolltolower="scroll_bottom"
      >
        <bargin-good
          :goodsList="goodList"
          v-if="goodList.length > 0"
          @goods_down="goods_down"
          @edit="edit"
          :timestamp="timestamp"
        ></bargin-good>
      </scroll-view>
      <!-- 商品展示区域 -->
      <view class="empty" style="text-align: center" v-if="goodList.length == 0"
        >暂无商品,去添加</view
      >
      <view class="toAssistant1" @tap="go_add_goods"> 添加商品 </view>
    </view>

    <view class="add_rule_container" v-if="rule_show">
      <!-- <van-field
        :value="value"
        placeholder="请选择砍价倒计时时间"
        :border="false"
        @change="onChange"
        label="砍价倒计时时间"
        disabled
        @tap="choose_time"
      /> -->
      <!-- <view
        class="start_pick_container"
        @tap="choose_time"
        style="margin-bottom: 10rpx"
      >
        <van-field
          :value="value"
          :border="false"
          label="砍价倒计时时间"
          disabled
        />
        <view v-if="!countdown" class="placeholder">请选择砍价倒计时时间</view>
        <view v-if="bargin_ruleForm.hour" class="placeholder">{{
          countdown
        }}</view>
      </view> -->
      <van-field
        :value="bargin_ruleForm.cut_item_title"
        placeholder="请输入自定义名称"
        :border="false"
        @change="change_cut_title"
        type="textarea"
        autosize
        label="自定义名称"
      />
      <van-field
        :value="bargin_ruleForm.need_times"
        placeholder="请输入次数"
        :border="false"
        @change="change_need_times"
        label="需要砍多少次"
      />
      <van-field
        :value="bargin_ruleForm.one_people_times"
        placeholder="请输入每人次数"
        :border="false"
        @change="change_per_times"
        label="每人次数(默认次数)"
      />

      <van-field
        :value="bargin_ruleForm.price"
        placeholder="请输入商品价格"
        :border="false"
        disabled
        label="商品价格"
      />
      <van-field
        :value="bargin_ruleForm.cut_price"
        placeholder="请输入保底价"
        :border="false"
        @change="change_cut_price"
        label="保底价"
      />
      <van-field
        :value="bargin_ruleForm.success_times"
        placeholder="请输入已成功次数"
        :border="false"
        @change="change_suc_times"
        label="成功次数"
      />
      <van-field
        :value="bargin_ruleForm.open_times"
        placeholder="请输入活动被开启次数"
        :border="false"
        @change="change_open_times"
        label="已开启次数"
      />
      <van-cell>
        <view slot="title">{{
          bargin_ruleForm.show_need_times == 1
            ? "显示砍价所需刀数"
            : "不显示所需刀数"
        }}</view>
        <view slot="">
          <van-switch
            :checked="bargin_ruleForm.show_need_times == 1 ? true : false"
            @change="change_show_need_times"
          />
        </view>
      </van-cell>
      <van-cell>
        <view slot="title">{{
          bargin_ruleForm.show_bj_price == 1 ? "显示保底价" : "不显示保底价"
        }}</view>
        <view slot="">
          <van-switch
            :checked="bargin_ruleForm.show_bj_price == 1 ? true : false"
            @change="change_show_bj_price"
          />
        </view>
      </van-cell>
      <view style="padding-left: 30upx">图片(图片大小不超过1m)</view>
      <view class="upload" style="padding-left: 20upx">
        <uImg
          ref="upimg"
          :canUploadFile="true"
          :limit="limit"
          :uploadFileUrl="uploadFileUrl"
          :uImgList.sync="uImgList"
          @uploadSuccess="uploadSuccess"
          :fileKeyName="name"
        />
        <!-- <van-button
          type="primary"
          size="mini"
          style="margin-left: 20upx"
          @tap="upload"
          >确定上传</van-button
        > -->
      </view>
      <div class="btn_box">
        <van-button @tap="submit" size="large" color="#f95a4b">提交</van-button>
        <van-button @tap="cancle_submit" size="large">返回</van-button>
      </div>
    </view>
    <van-dialog id="van-dialog" />
    <van-toast id="van-toast" />
  </view>
</template>

<script>
import BarginGood from "../near_mch_bargin/components/can_bargin_goods";
import uImg from "../../components/zhtx-uploadImg/zhtx-uploadImg";
import {
  cutprice_list,
  query_xp_goods,
  cutprice_statusupdate,
  item_save,
} from "../../utils/imapi";
import Dialog from "../../wxcomponents/vant/dialog/dialog";
export default {
  props: {},
  components: {
    BarginGood,
    uImg,
  },
  data() {
    return {
      value: "",
      option1: [
        { text: "全部", value: 0 },
        { text: "待付款", value: 1 },
        { text: "待发货", value: 2 },
        { text: "交易完成", value: 3 },
        { text: "退款退货中", value: 4 },
        { text: "已退款", value: 5 },
        { text: "交易取消", value: 6 },
      ],
      goodList: [],
      value1: 0,
      queryparam: {
        page_index: 1,
        page_size: 10,
        status: "",
        create_time_start: "",
        create_time_end: "",
        cut_price: "",
        cut_mode: "2",
      },
      bargin_ruleForm: {},
      rule_show: false,
      uImgList: [],
      value: "",
      uploadFileUrl:
        "https://api.comm.t.xingkonglian.net:8098/api/v1/oss/upload_starx",
      limit: 1,
      name: "file",
      show_time_picker: false,
      minHour: "00",
      maxHour: "23",
      countdown: "",
      timestamp:''
    };
  },
  computed: {},
  watch: {},
  onLoad() {},
  onReady() {},
  onShow() {
    this.cutprice_list();
  },
  methods: {
    async cutprice_list() {
      const timestamp = Date.parse(new Date()).toString()
      try {
        const data = await cutprice_list(this.queryparam);
        console.log(data);
        if (data.code == 0) {
          this.timestamp=timestamp
          this.goodList = data.data;
          console.log(this.goodList);
        }
      } catch (error) {}
    },
    status_change(e) {
      console.log(e.detail);
    },
    inputchange(e) {
      console.log(e.detail);
      this.queryparam.title = e.detail.value;
    },
    reset() {
      this.queryparam = {
        goods_status: "",
        page_index: 1,
        page_size: 10,
        title: "",
        load_spec: "1",
      };
      this.query_all_goods();
    },
    thequery() {
      console.log(this.queryparam.title);
      this.query_all_goods();
    },
    async scroll_bottom() {
      console.log("触底了");
      this.queryparam.page_index++;
      try {
        const data = await query_all_goods(this.queryparam);
        this.$toast.loading("加载中");
        if (data.code == 0) {
          this.goodList.push(...data.data);
          console.log(data);
          if (data.data.length == 0) {
            this.queryparam.page_index--;
            this.$toast("全部加载");
          }
        }
      } catch (error) {}
    },
    go_add_goods() {
      uni.navigateTo({
        url: "/pages/mch_goods/mch_goods?from=twitter",
      });
    },
    goods_down(e) {
      console.log(e);
      const status = e.status == "1" ? "0" : "1";
      const text = e.status == "1" ? "下架" : "上架";
      Dialog.confirm({
        title: "确定" + text + "此砍价商品?",
      })
        .then(async () => {
          // on confirm
          const data = await cutprice_statusupdate({
            id: e.id,
            status,
          });
          if (data.code == 0) {
            this.$toast.success("操作成功");
            this.cutprice_list();
          }
        })
        .catch(() => {
          // on cancel
        });
    },
    edit(e) {
      if (e.status == "1") {
        return this.$toast("请先下架此商品再进行编辑操作");
      }
      this.bargin_ruleForm = e;
      this.uImgList = [];
      this.uImgList.push(e.pos_pic);
      this.rule_show = true;
    },
    change_need_times(e) {
      this.bargin_ruleForm.need_times = e.detail;
    },
    change_per_times(e) {
      this.bargin_ruleForm.one_people_times = e.detail;
    },
    change_cut_title(e) {
      this.bargin_ruleForm.cut_item_title = e.detail;
    },
    change_cut_price(e) {
      this.bargin_ruleForm.cut_price = e.detail;
    },
    change_suc_times(e) {
      this.bargin_ruleForm.success_times = e.detail;
    },
    change_open_times(e) {
      this.bargin_ruleForm.open_times = e.detail;
    },
    change_show_need_times(e) {
      console.log(e);
      this.bargin_ruleForm.show_need_times = e.detail ? 1 : 0;
    },
    change_show_bj_price(e) {
      console.log(e);
      this.bargin_ruleForm.show_bj_price = e.detail ? 1 : 0;
    },
    uploadSuccess(result) {
      if (result.statusCode == 200) {
        //上传成功的回调处理
        console.log(result.data);
        const img = JSON.parse(result.data).data;
        console.log(img);
        this.bargin_ruleForm.pos_pic = img;
        console.log(result);
      } else {
      }
    },
    async submit() {
      console.log(this.bargin_ruleForm);
      if (
        this.bargin_ruleForm.need_times == "" ||
        this.bargin_ruleForm.one_people_times == "" ||
        this.bargin_ruleForm.cut_item_title == "" ||
        this.bargin_ruleForm.pos_pic == "" ||
        this.bargin_ruleForm.price == "" ||
        (this.bargin_ruleForm.open_times == "" &&
          this.bargin_ruleForm.open_times != 0) ||
        (this.bargin_ruleForm.success_times == "" &&
          this.bargin_ruleForm.success_times != 0) ||
        this.bargin_ruleForm.cut_price == ""
      ) {
        this.$toast("请填写完整数据");
        return;
      }
      this.bargin_ruleForm.cut_price = Number(this.bargin_ruleForm.cut_price);
      this.bargin_ruleForm.hour = Number(this.bargin_ruleForm.hour);
      this.bargin_ruleForm.minutes = Number(this.bargin_ruleForm.minutes);
      this.bargin_ruleForm.need_times = Number(this.bargin_ruleForm.need_times);
      this.bargin_ruleForm.one_people_times = Number(
        this.bargin_ruleForm.one_people_times
      );
      this.bargin_ruleForm.open_times = Number(this.bargin_ruleForm.open_times);
      this.bargin_ruleForm.price = Number(this.bargin_ruleForm.price);
      this.bargin_ruleForm.success_times = Number(
        this.bargin_ruleForm.success_times
      );
      this.bargin_ruleForm.show_need_times = Number(
        this.bargin_ruleForm.show_need_times
      );
      this.bargin_ruleForm.show_bj_price = Number(
        this.bargin_ruleForm.show_bj_price
      );
      this.bargin_ruleForm.limit_radius_pub = Number(
        this.bargin_ruleForm.limit_radius_pub
      );
      try {
        const data = await item_save(this.bargin_ruleForm);
        if (data.code == 0) {
          this.$toast.success("编辑成功");
          this.rule_show = false;
          this.cutprice_list();
        }
      } catch (err) {
        console.log(err);
        this.$toast.success("编辑失败");
      }
    },
    cancle_submit() {
      console.log("111");
      this.rule_show = false;
    },
    upload() {
      this.$refs.upimg.upload();
    },
  },
  onReachBottom() {},
  onHide() {},
};
</script>

<style scoped lang="scss" scoped>
.query_container {
  .query_box {
    width: 750upx;
    height: 220upx;
    background: #f0eefd;
    padding: 32upx;
    /deep/ .van-cell {
      height: 68upx;
      background: #ffffff;
      border-radius: 38upx;
      display: flex;
      align-items: center;
      .input {
        height: 100%;
        font-size: 24upx;
      }
    }
    .query_btn_box {
      margin-top: 20upx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      /deep/ .van-dropdown-menu {
        width: 320upx;
        height: 68upx;
        background: #ffffff;
        border-radius: 34upx;
      }
      /deep/ .reset_btn {
        width: 320upx;
        height: 68upx;
        background: #ffffff;
        border-radius: 34upx;
        border: 2upx solid #6d55ed;
        text-align: center;
        line-height: 68upx;
        font-size: 28upx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #6d55ed;
      }
      /deep/ .query_btn {
        width: 320upx;
        height: 68upx;
        background: #6d55ed;
        border-radius: 34upx;
        text-align: center;
        line-height: 68upx;
        font-size: 28upx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #fff;
      }
    }
  }
}
.toAssistant1 {
  position: fixed;
  bottom: 10upx;
  width: 690upx;
  left: 30upx;
  /* right: 0; */
  height: 80upx;
  background: #f03752;
  border-radius: 50upx;
  text-align: center;
  line-height: 80upx;
  font-size: 32upx;
  color: #fff;
  font-weight: 600;
  font-family: PingFangSC-Semibold, PingFang SC;
  z-index: 99;
  /* margin-top: 60upx; */
}
.btn_box {
  padding: 0 20px;
  margin-top: 20upx;
  /deep/ .van-button:first-child {
    margin-bottom: 20upx;
  }
}
.upload {
  display: flex;
  align-items: center;
}
</style>
